package com.example.myapplication.Activity;

import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;

import androidx.activity.EdgeToEdge;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import com.example.myapplication.Fragment.CommunityFragment;
import com.example.myapplication.Fragment.HomeFragment;
import com.example.myapplication.Fragment.MineFragment;
import com.example.myapplication.Fragment.OrderFragment;
import com.example.myapplication.R;

public class MainActivity extends AppCompatActivity {
    TextView tvTitle;
    RadioGroup rgTab;
    RadioButton rbHome,rbCommunity,rbOrder,rbMe;
    //Fragment管理类
    FragmentManager fragmentManager;
    FragmentTransaction fragmentTransaction;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EdgeToEdge.enable(this);
        setContentView(R.layout.activity_main);
        //初始化Fragment管理类
        fragmentManager = getSupportFragmentManager();
        //获取Fragment事务操作,开启事务
        fragmentTransaction = fragmentManager.beginTransaction();
        //事务处理
        //1、R.id.fl_main站位布局位置，2、实例化的Fragment
        fragmentTransaction.replace(R.id.fl_main,new MineFragment());
        //提交事务
        fragmentTransaction.commit();
        initView();
        rbMe.setChecked(true);



    }
    private void initView() {
        rgTab = findViewById(R.id.rg_tab);
        rbHome = findViewById(R.id.rb_tab_home);
        rbCommunity = findViewById(R.id.rb_tab_community);
        rbOrder = findViewById(R.id.rb_tab_order);
        rbMe = findViewById(R.id.rb_tab_me);
        //选项选中改变事件监听
        rgTab.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup radioGroup, int id) {
                fragmentTransaction = fragmentManager.beginTransaction();
                //1、设置所有图标和文本的默认颜色
                initImageAndText();
                //判断选中的选项 id 代表被选中的选项id
                /*switch (id){
                    case R.id.rb_tab_home:
                        //选中了第一个选项
                        //2、设置选中项的图标和文本颜色
                        rbHome.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.icon_home_true),null,null);
                        rbHome.setTextColor(getResources().getColor(R.color.main));
                        fragmentTransaction.replace(R.id.fl_main,new HomeFragment());
                        Toast.makeText(MainActivity.this, "选中了首页" , Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_tab_community:
                        //选中了第一个选项
                        //2、设置选中项的图标和文本颜色
                        rbCommunity.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.icon_community_true),null,null);
                        rbCommunity.setTextColor(getResources().getColor(R.color.main));
                        fragmentTransaction.replace(R.id.fl_main,new CommunityFragment());
                        Toast.makeText(MainActivity.this, "选中了吃货驾到" , Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_tab_order:
                        //选中了第一个选项
                        //2、设置选中项的图标和文本颜色
                        rbOrder.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.icon_order_true),null,null);
                        rbOrder.setTextColor(getResources().getColor(R.color.main));
                        fragmentTransaction.replace(R.id.fl_main,new OrderFragment());
                        Toast.makeText(MainActivity.this, "选中了订单" , Toast.LENGTH_SHORT).show();
                        break;
                    case R.id.rb_tab_me:
                        //选中了第一个选项
                        //2、设置选中项的图标和文本颜色
                        rbMe.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.mipmap.icon_me_true),null,null);
                        rbMe.setTextColor(getResources().getColor(R.color.main));
                        fragmentTransaction.replace(R.id.fl_main,new MineFragment());
                        Toast.makeText(MainActivity.this, "选中了个人中心" , Toast.LENGTH_SHORT).show();
                        break;
                }*/
                if(id==R.id.rb_tab_home){
                    rbHome.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_home_true),null,null);
                    rbHome.setTextColor(getResources().getColor(R.color.main));
                    fragmentTransaction.replace(R.id.fl_main,new HomeFragment());
                    Toast.makeText(MainActivity.this, "选中了首页" , Toast.LENGTH_SHORT).show();
                } else if (id==R.id.rb_tab_community) {
                    rbCommunity.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_community_true),null,null);
                    rbCommunity.setTextColor(getResources().getColor(R.color.main));
                    fragmentTransaction.replace(R.id.fl_main,new CommunityFragment());
                    Toast.makeText(MainActivity.this, "选中了吃货驾到" , Toast.LENGTH_SHORT).show();
                } else if (id==R.id.rb_tab_order) {
                    rbOrder.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_order_true),null,null);
                    rbOrder.setTextColor(getResources().getColor(R.color.main));
                    fragmentTransaction.replace(R.id.fl_main,new OrderFragment());
                    Toast.makeText(MainActivity.this, "选中了订单" , Toast.LENGTH_SHORT).show();
                } else if (id==R.id.rb_tab_me) {
                    rbMe.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_me_true),null,null);
                    rbMe.setTextColor(getResources().getColor(R.color.main));
                    fragmentTransaction.replace(R.id.fl_main,new MineFragment());
                    Toast.makeText(MainActivity.this, "选中了个人中心" , Toast.LENGTH_SHORT).show();
                }

                fragmentTransaction.commit();
            }
        });
    }
    private void initImageAndText(){
        //默认图标
        rbHome.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_home_false),null,null);
        rbCommunity.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_community_false),null,null);
        rbOrder.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_order_false),null,null);
        rbMe.setCompoundDrawablesRelativeWithIntrinsicBounds(null,getResources().getDrawable(R.drawable.icon_me_false),null,null);
        //默认文字颜色
        rbHome.setTextColor(getResources().getColor(R.color.black));
        rbCommunity.setTextColor(getResources().getColor(R.color.black));
        rbOrder.setTextColor(getResources().getColor(R.color.black));
        rbMe.setTextColor(getResources().getColor(R.color.black));
    }

}